<?php

namespace App\Http\Controllers\Api\V1;
use App\Models\News;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Api\V1\Resources\NewsListResource;
use App\Http\Controllers\Api\V1\Resources\NewsResource;
use Illuminate\Database\Eloquent\Builder;

class NewsController extends Controller
{
	public function index()
	{


		/*$news = News::query()->where('is_examine',true)->where("created_at",">",Carbon::yesterday())->orderByDesc("id")->get();
		if(empty($news)){
			$news = News::query()->where('is_examine',true)->where("created_at",">",Carbon::yesterday()->subDays(1))->orderByDesc("id")->get();

		}*/

		$news = [];
		$today = "";
		$news = $this->rewss($news,$today);

		return NewsListResource::collection($news);
	}

	public function rewss($news,$today){


		if($today){

			$news = News::query()->where('is_examine',true)->where("created_at",">",$today->subDays(1))->orderByDesc("id")->get();
			$today = $today->subDays(1);
		}else{
			$today = Carbon::yesterday();
			$news = News::query()->where('is_examine',true)->where("created_at",">",Carbon::yesterday())->orderByDesc("id")->get();
		}


		if(empty($news->toArray())){

			return $this->rewss($news,$today);
		}

		return $news;
	
			
		

	}

	public function show($id)
	{
		$new = News::find($id);
		return new NewsResource($new);
	}

	
}
